/*
* Conditions Of Use
*
* This software was developed by employees of the Sigmatrix(Beijing) Corporation.
* This software is provided by sigmatrix as a service and is expressly
* provided "AS IS."  Sigmatrix MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
* AND DATA ACCURACY.  Sigmatrix does not warrant or make any representations
* regarding the use of the software or the results thereof, including but
* not limited to the correctness, accuracy, reliability or usefulness of
* the software.
*
* Permission to use this software is contingent upon your acceptance
* of the terms of this agreement.
*
*/
package com.sigmatrix.h5.dao.impl.common.impl;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.util.CollectionUtils;

import com.sigmatrix.h5.dao.impl.common.BaseDao;
import com.sigmatrix.h5.enums.TableEnum;
import com.sigmatrix.h5.exception.DaoException;
import com.sigmatrix.h5.utils.Utils;

/**
 *@ClassName: BaseDaoImpl
 *@Description: TODO()
 *@date 2017年7月13日 上午11:43:29
 */
public class BaseDaoImpl<T extends Serializable> implements BaseDao<T> {
	
	@Autowired
	protected transient JdbcTemplate jdbcTemplate;

	/**是否为分表策略,为true,表名加上传递的企业code*/
	public static final boolean ISSUBMETER = true;
	
	/**
	 *@Title: getEntTableName
	 *@Description: 根据表策略(分库/表)获得表名
	 *@return String
	 *@date 2017年7月13日 上午11:52:48
	 *@param eseCode
	 *@param tName
	 */
	protected String getTname(final String eseCode, final String tName) {
		
		final StringBuilder sb = new StringBuilder();
		if(ISSUBMETER) { // 分表
			if(Utils.isNotEmpty(eseCode)){//企业code不为空
				sb.append("_").append(eseCode);
			}
		}
		sb.append(tName);
		return sb.toString();
	}
	
	/**
	 *@Title: getEntTableName
	 *@Description: 获得表名,根据表Enum
	 *@return String
	 *@date 2017年7月31日 下午3:04:01
	 *@param enterpriseCode
	 *@param tableEnum
	 */
	protected String getTname(final String enterpriseCode, final TableEnum tableEnum) {

		final StringBuilder sb = new StringBuilder();
		if (ISSUBMETER) { // 分表
			if (tableEnum.getIsSubmeter()) {
				if (StringUtils.isNotBlank(enterpriseCode)) {
					sb.append("_").append(enterpriseCode);
				}
			}
		}
		final String tableName = tableEnum.getName();
		sb.append(tableName);
		return sb.toString();
	}
	
	/**
	 *@Title: getIntArray
	 *@Description: 
	 *@return int[]
	 *@author youjun
	 *@date 2017年8月28日 下午5:26:41
	 *@param argTypeList
	 *@return
	 */
	protected int[] getIntArray(final List<Integer> argTypeList) {
		if(!CollectionUtils.isEmpty(argTypeList)) {
			final int[] argTypeArray = new int[argTypeList.size()];
			int i = 0;
			for(final Integer argType:argTypeList) {
				argTypeArray[i] = argType;
				i++;
			}
			return argTypeArray;
		}
		return null;
	}
	
	/**
	 * (non-Javadoc)
	 *@Title: saveReturnKey
	 *@Description: 保存返回id
	 *@see com.sigmatrix.sc.core.dao.BaseDao#saveReturnKey(java.lang.String, java.lang.Object[])
	 *@date 2017年7月21日 上午12:46:14
	 */
	public Long saveReturnKey(final String sql,final Object[] params) throws DaoException{
		try {
			final KeyHolder keyHolder = new GeneratedKeyHolder();
			jdbcTemplate.update(new PreparedStatementCreator() {
				@Override
				public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
					final PreparedStatement pstmt = con.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
					int index = 1;
					for (final Object value : params) {
						pstmt.setObject(index, value);
						index++;
					}
					return pstmt;
				}
			}, keyHolder);
			return keyHolder.getKey().longValue();
		} catch (DataAccessException e) {
			throw new DaoException(e);
		}
	}

	
}
